package com.wsjc.app.data.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wsjc.app.data.App;
import com.wsjc.common.core.constants.CommonConst;
import com.wsjc.manager.entity.WjDataLog;
import com.wsjc.manager.service.routine.datalog.WjDataLogService;
import com.wsjc.manager.service.rpc.data.WjHospitalDataService;

/**
 * <b>功能说明:
 * TODO... 无注释
 * 
 */
@Service("dataPersist")
public class DataPersist {

	private static Logger logger = LoggerFactory.getLogger(DataPersist.class);

    @Autowired
    private WjDataLogService wjDataLogService;

	@Autowired
	private WjHospitalDataService wjHospitalDataService;
	
	
    /**
     * 更新地图更新状态.<br/>
     * @param id
     * @param status
     */
    public void updateDataLog(WjDataLog wjDataLog, String mapStatus) {
    	wjDataLog.setMapStatus(mapStatus);
    	wjDataLogService.updateData(wjDataLog);
    }


    /**
     * 从数据库中取一次数据用来当系统启动时初始化
     */
    public void initSolrDataLogFromDB() throws InterruptedException{
    	
    	logger.info("===>init get solr data log from database");

        // 组装查询条件，通知状态不成功，并且还没有超过最大通知次数的
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("dataStatus", CommonConst.DATA_STATUS_CREATE); // 创建状态

        List<WjDataLog> dataLogList = wjDataLogService.listBy(paramMap);
        if (dataLogList == null || dataLogList.isEmpty()) {
        	logger.info("app-data|initMapDataLogFromDB| the dataLogList is null or empty..");
            return;
        }

        for (WjDataLog wjDataLog : dataLogList) {
				App.tasks.put(new DataTask(wjDataLog)); // 添加到通知队列(第一次通知)
        }
        logger.info("app-data|initSolrDataLogFromDB| init is successful, size: {}", dataLogList.size());
    }


    /**
     * 更新地图
     * @param dataId
     * @param dataType
     * @param hospitalId
     */
	public void flushData(String logId, String dataId, String dataType, String hospitalId) {
		
		WjDataLog wjDataLog = wjDataLogService.getDataById(logId);
		if(wjDataLog.getDataStatus().equals(CommonConst.DATA_STATUS_SUCCESS)){
			return ;
		}
		/**
		 * 1，数据插入到医院详情表
		 * 2，新数据上传到solr
		 */
		wjHospitalDataService.flushData(hospitalId, dataType, dataId);
		
	}


}
